package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.TobeAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.TobeModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.XToast;

import java.util.List;

public class TobeActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private TobeAdapter adapter;

    private boolean isUp = false;
    private int fristID = 1;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tobe);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        loadNewData();
    }

    private void initView() {
        setTitleText("快速咨询");
        imageView = (ImageView) findViewById(R.id.tobe_iv);
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.tobe_list);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                loadNewData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                isUp = true;
                setPost(fristID, fristID + 9, isUp);
            }
        });
        listView = pullToRefreshListView.getRefreshableView();
        listView.setDivider(null);
        adapter = new TobeAdapter(this, null, R.layout.tobe_item);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    private void loadNewData() {
        isUp = false;
        if (this.adapter != null) {
            int maxNum = 10;
            int count = adapter.getCount();
            if (count > 10) {
                maxNum += adapter.getCount() - 10;
            }
            fristID = 1;
            setPost(fristID, maxNum, isUp);
        }
    }

    private void setPost(int fristiD, int maxNum, final boolean isUp) {
        TobeModel.body body = new TobeModel.body();
        body.setSinceId(fristiD);
        body.setMaxId(maxNum);
        HttpUtil.doPost(Constants.Url.CONSULTTATION_LIST, body, new HttpResponse(this,TobeModel.class) {
            @Override
            public void onResult(Object obj) {
                TobeModel s = (TobeModel) obj;
//                LogUtil.e(s);
                int rows = s.getRows();
                List<TobeModel.DataBean> data = s.getData();
                fristID += rows;
                if (isUp)
                    adapter.addRes(data);
                else
                    adapter.updateRes(data);
                if (rows == 0) {
                    if (adapter.getCount() == 0) {
                        imageView.setVisibility(View.VISIBLE);
                    } else {
                        imageView.setVisibility(View.GONE);
                    }
                    if (isUp)
                        XToast.showToast(TobeActivity.this, "没有更多数据");
                } else {
                    imageView.setVisibility(View.GONE);
                }
            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                pullToRefreshListView.onRefreshComplete();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clear();
            adapter = null;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        TobeModel.DataBean item = adapter.getItem(i - 1);
        IntentManager.goAnswerActivity(this,item);
    }
}
